import React, {Component} from 'react';
import {View, Text, AsyncStorage} from 'react-native';
import Nav from './src/nav';
import Geo from './src/utils/Geo';
import RootStore from './src/mobx';
import UserStore from './src/mobx/userStore';
import {Provider} from 'mobx-react';
import JMessage from './src/utils/JMessage';
import {inject, observer} from 'mobx-react';

class App extends Component {
  state = {
    isInitGeo: false,
  };
  async componentDidMount() {
    // 获取缓存中的用户数据
    const strUserInfo = await AsyncStorage.getItem('userinfo');
    const userinfo = strUserInfo ? JSON.parse(strUserInfo) : {};
    // 判断里边有没有token
    if (userinfo.token) {
      RootStore.setUserInfo(userinfo.mobile, userinfo.token, userinfo.userId);
      JMessage.init();
      const res1 = await this.jglogin(userinfo.userId, userinfo.mobile);
    }

    await Geo.initGeo();
    this.setState({isInitGeo: true});

    //  const res = await Geo.getCityByLocation();
    //  console.log(res);
  }
  // 极光登录
  jglogin = async (username, password) => {
    return await JMessage.login(username, password);
  };

  render() {
    return (
      <View style={{flex: 1}}>
        <Provider RootStore={RootStore} UserStore={UserStore}>
          {this.state.isInitGeo ? <Nav /> : <></>}
        </Provider>
      </View>
    );
  }
}
export default App;
